import React, { Component } from 'react'

const UserNameContext = React.createContext()
export default class A extends Component {
  state = {
    username: 'MyName'
  }

  render() {
    const { state } = this
    return (
      <div>
        A：
        {/* value也可以传递对象 */}
        <UserNameContext.Provider value={state}>
          <B />
        </UserNameContext.Provider>
      </div>
    )
  }
}

class B extends A {
  render() {
    return (
      <div>
        B：
        <C />
      </div>
    )
  }
}

class C extends B {
  // 声明才能接收Context中定义的数据
  static contextType = UserNameContext

  render() {
    const { username } = this.context
    return <div>C：{username}</div>
  }
}
